package com.bear.udisk.service.impl;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bear.udisk.mapper.FileinfoMapper;
import com.bear.udisk.model.Fileinfo;
import com.bear.udisk.service.FileinfoService;

@Service
public class FileinfoServiceImpl implements FileinfoService{
	@Autowired
	private FileinfoMapper fileinfoMapper;
	
	@Override
	public List<Fileinfo> findByUserId(int userId) {
		List<Fileinfo> list = fileinfoMapper.findByUserId(userId);
		return list;
	}

	@Override
	public int add(Fileinfo file) {
		int count = fileinfoMapper.add(file);
		return count;
	}

	@Override
	public Fileinfo findByFileCode(String fileCode) {
		Fileinfo fileinfo = fileinfoMapper.findByFileCode(fileCode);
		return fileinfo;
	}

	/**
	 * a.删除服务器端的文件
	 * b.删除数据库中记录
	 */
	@Override
	public int delete(int fileId) {
		Fileinfo fileinfo = fileinfoMapper.findByFileId(fileId);
		File file = new File(fileinfo.getFilePath());
		if(file.exists()){
			file.delete();//删除文件
		}
		int count = fileinfoMapper.delete(fileId);
		return count;
	}

}
